package com.homework3;

import java.util.*;

public class Test03 {
    public static void main(String[] args) {
        String[] hashSet2={"苹果","香蕉","西瓜","橘子"};
        HashMap<String, Object> map = new HashMap<>();
        for (Object o : hashSet2) {
            StringBuilder str = new StringBuilder();
            while (str.length()<8){
                int rd= new Random().nextInt(9) + 1;
                str.append(rd);
                if(map.containsKey(str)){
                    str.delete(0,str.length());
                }
            }
            map.put(String.valueOf(str),o);
        }
        System.out.println(map);
        System.out.println("请输入8位商品号来查询商品");
        String s = new Scanner(System.in).nextLine();
        char[] chars = s.toCharArray();
        if(chars.length<0||chars.length>8||s==" "){
            try {
                throw new Exception("商品编号错误");
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                System.out.println("内存释放.......");
            }
        }else {
            if(map.containsKey(s)){
                System.out.println(map.get(s));
            }else {
                System.out.println("商品不存在");
            }
        }
    }
}
